package per.jdz.retryer.strategy;

import java.util.concurrent.TimeUnit;

import per.jdz.retryer.listener.RetryerListener;

public abstract class AbstractRetryerStrategy {
    protected int count;
    protected final long time;
    protected final TimeUnit timeUnit;
    protected RetryerListener retryerListener;
    
    public AbstractRetryerStrategy(int count, long time, TimeUnit timeUnit) {
        this.count = count;
        this.time = time;
        this.timeUnit = timeUnit;
    }

    public abstract void reset();
    
    abstract boolean exec();

    // 模板方法
    public boolean execute() {
        final boolean result = exec();
        if (result) {
            execRetryerListener();
        }
        return result;
    }

    // 执行监听器
    void execRetryerListener() {
        if (null != retryerListener) {
            // 将重试策略组件放入监听器
            retryerListener.exec(this);
        }
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setRetryerListener(RetryerListener retryerListener) {
        this.retryerListener = retryerListener;
    }

    public int getCount() {
        return count;
    }

    public long getTime() {
        return time;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public RetryerListener getRetryerListener() {
        return retryerListener;
    }

}